package jltx.fragmentpageframework.zihu;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;

import java.util.ArrayList;
import java.util.List;

import cn.jltx.support.SupportAppCompatActivity;
import cn.jltx.utils.ViewHelper;
import jltx.fragmentpageframework.R;
import jltx.fragmentpageframework.widget.BottomBar;
import jltx.fragmentpageframework.widget.BottomBarTab;
import jltx.fragmentpageframework.zihu.adapter.ZhiHuMainTabAdapter;
import jltx.fragmentpageframework.zihu.fragment.BaseZhiHuTabFragment;
import jltx.fragmentpageframework.zihu.fragment.DiscoverFragment;
import jltx.fragmentpageframework.zihu.fragment.IndexFragment;
import jltx.fragmentpageframework.zihu.fragment.MoreFragment;
import jltx.fragmentpageframework.zihu.fragment.NotificationFragment;
import jltx.fragmentpageframework.zihu.fragment.ShopFragment;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2017/06/26.
 */

public class ZhiHuActivity extends SupportAppCompatActivity{

    private List<BaseZhiHuTabFragment> fragmentList;
    private ZhiHuMainTabAdapter pagerAdapter;

    private ViewPager mainPager;
    private BottomBar bottomBar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhihu);

        mainPager = ViewHelper.findViewById(this,R.id.zhihu_main_pager);
        bottomBar = ViewHelper.findViewById(this,R.id.zhihu_main_bottom_bar);

        if(savedInstanceState == null){
            fragmentList = new ArrayList<>();

            IndexFragment indexFragment = IndexFragment.newInstance();
            fragmentList.add(indexFragment);
            bottomBar.addItem(new BottomBarTab(this).setIconImageResource(R.drawable.ic_bottomtabbar_feed).setIconImageWidthAndHeight(R.dimen.bottombar_tab_zihu_width_height,R.dimen.bottombar_tab_zihu_width_height));

            DiscoverFragment discoverFragment = DiscoverFragment.newInstance();
            fragmentList.add(discoverFragment);
            bottomBar.addItem(new BottomBarTab(this).setIconImageResource(R.drawable.ic_bottomtabbar_discover).setIconImageWidthAndHeight(R.dimen.bottombar_tab_zihu_width_height,R.dimen.bottombar_tab_zihu_width_height));

            ShopFragment shopFragment = ShopFragment.newInstance();
            fragmentList.add(shopFragment);
            bottomBar.addItem(new BottomBarTab(this).setIconImageResource(R.drawable.ic_bottomtabbar_shop).setIconImageWidthAndHeight(R.dimen.bottombar_tab_zihu_width_height,R.dimen.bottombar_tab_zihu_width_height));

            NotificationFragment notificationFragment = NotificationFragment.newInstance();
            fragmentList.add(notificationFragment);
            bottomBar.addItem(new BottomBarTab(this).setIconImageResource(R.drawable.ic_bottomtabbar_notification).setIconImageWidthAndHeight(R.dimen.bottombar_tab_zihu_width_height,R.dimen.bottombar_tab_zihu_width_height));

            MoreFragment moreFragment = MoreFragment.newInstance();
            fragmentList.add(moreFragment);
            bottomBar.addItem(new BottomBarTab(this).setIconImageResource(R.drawable.ic_bottomtabbar_more).setIconImageWidthAndHeight(R.dimen.bottombar_tab_zihu_width_height,R.dimen.bottombar_tab_zihu_width_height));

        }else {
            fragmentList = new ArrayList<>();

            BaseZhiHuTabFragment indexFragment = findFragment(IndexFragment.class);
            fragmentList.add(indexFragment);
            bottomBar.addItem(new BottomBarTab(this).setIconImageResource(R.drawable.ic_bottomtabbar_feed).setIconImageWidthAndHeight(R.dimen.bottombar_tab_zihu_width_height,R.dimen.bottombar_tab_zihu_width_height));

            BaseZhiHuTabFragment discoverFragment = findFragment(DiscoverFragment.class);
            fragmentList.add(discoverFragment);
            bottomBar.addItem(new BottomBarTab(this).setIconImageResource(R.drawable.ic_bottomtabbar_discover).setIconImageWidthAndHeight(R.dimen.bottombar_tab_zihu_width_height,R.dimen.bottombar_tab_zihu_width_height));

            BaseZhiHuTabFragment shopFragment = findFragment(ShopFragment.class);
            fragmentList.add(shopFragment);
            bottomBar.addItem(new BottomBarTab(this).setIconImageResource(R.drawable.ic_bottomtabbar_shop).setIconImageWidthAndHeight(R.dimen.bottombar_tab_zihu_width_height,R.dimen.bottombar_tab_zihu_width_height));

            BaseZhiHuTabFragment notificationFragment = findFragment(NotificationFragment.class);
            fragmentList.add(notificationFragment);
            bottomBar.addItem(new BottomBarTab(this).setIconImageResource(R.drawable.ic_bottomtabbar_notification).setIconImageWidthAndHeight(R.dimen.bottombar_tab_zihu_width_height,R.dimen.bottombar_tab_zihu_width_height));

            BaseZhiHuTabFragment moreFragment = findFragment(MoreFragment.class);
            fragmentList.add(moreFragment);
            bottomBar.addItem(new BottomBarTab(this).setIconImageResource(R.drawable.ic_bottomtabbar_more).setIconImageWidthAndHeight(R.dimen.bottombar_tab_zihu_width_height,R.dimen.bottombar_tab_zihu_width_height));

        }

        pagerAdapter = new ZhiHuMainTabAdapter(getSupportFragmentManager(),fragmentList);
        mainPager.setAdapter(pagerAdapter);
        mainPager.setOffscreenPageLimit(fragmentList.size());
        bottomBar.setOnTabSelectedListener(onTabSelectedListener);
    }

    private BottomBar.OnTabSelectedListener onTabSelectedListener = new BottomBar.OnTabSelectedListener() {
        @Override
        public void onTabSelected(int position, int prePosition) {
            mainPager.setCurrentItem(position,false);
        }

        @Override
        public void onTabUnselected(int position) {

        }

        @Override
        public void onTabReselected(int position) {

        }
    };
}
